package com.gsbelarus.gedemin.salary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.database.RealmHelper;
import com.gsbelarus.gedemin.salary.entity.PayslipDetailCard;
import com.gsbelarus.gedemin.salary.util.CurrencyHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

public class PayslipDetailAdapter extends RecyclerView.Adapter<PayslipDetailAdapter.ItemViewHolder> {

    private List<PayslipDetailCard> cardsList;
    private Calendar date;

    private CurrencyHelper currencyHelper;
    private RealmHelper realmHelper;

    public PayslipDetailAdapter(Context context, RealmHelper realmHelper) {
        this.realmHelper = realmHelper;
        cardsList = new ArrayList<>();
        currencyHelper = CurrencyHelper.getInstance(context);
    }

    @Override
    public PayslipDetailAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.payslip_detail_cards_item, parent, false));
    }

    @Override
    public void onBindViewHolder(PayslipDetailAdapter.ItemViewHolder holder, int position) {

        PayslipDetailCard item = cardsList.get(position);

        holder.cardHeaderText.setText(item.getName());
        holder.cardHeaderSum.setText(
                currencyHelper.getConvertedSumString(realmHelper, date, item.getSum()) + currencyHelper.getKindOfCurrencyText(" "));

        holder.cardContent.removeAllViews();
        RelativeLayout contentItem;
        for (Map.Entry<String, Double> entry : item.getItems().entrySet()) {
            contentItem = (RelativeLayout) LayoutInflater.from(holder.cardContent.getContext())
                    .inflate(R.layout.payslip_detail_cards_item_content, holder.cardContent, false);

            TextView contentText = (TextView) contentItem.findViewById(R.id.payslip_detail_cards_content_text);
            TextView contentSum = (TextView) contentItem.findViewById(R.id.payslip_detail_cards_content_sum);

            contentText.setText(entry.getKey());
            contentSum.setText(
                    currencyHelper.getConvertedSumString(realmHelper, date, entry.getValue()));

            holder.cardContent.addView(contentItem);
        }
    }

    @Override
    public int getItemCount() {
        return cardsList.size();
    }

    public void setList(Calendar date, List<PayslipDetailCard> cardsList) {
        this.date = date;
        this.cardsList = cardsList;
        notifyDataSetChanged();
    }

    public void clear() {
        cardsList.clear();
        notifyDataSetChanged();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView cardHeaderText;
        private TextView cardHeaderSum;
        private LinearLayout cardContent;

        public ItemViewHolder(View v) {
            super(v);
            cardHeaderText = (TextView) v.findViewById(R.id.payslip_detail_cards_header_text);
            cardHeaderSum = (TextView) v.findViewById(R.id.payslip_detail_cards_header_sum);
            cardContent = (LinearLayout) v.findViewById(R.id.payslip_detail_cards_content);
        }
    }
}
